clear
*load the file
use j2j.dta

*Should have loaded the basic file by now, so make sure you do that
rename geography statecode
drop if industry=="00"
sort statecode industry year quarter
gen time = yq(year,quarter)
format %tq time
egen id = group(statecode industry)
tsset id time

egen indgroup = group(industry)
egen statebytime = group(statecode time)
egen indbytime = group(industry time)

*In the quarterly data we can identify exactly when the laws when into place

gen HasBan = 0
gen EverBan = 0
gen SinceBan = .
*California was effective Jan 1 2012, so Q1
replace HasBan = 1 if statecode==06 & ((year>=2012 & quarter>=1) | (year>=2013))
replace EverBan = 1 if statecode==06
replace SinceBan = 0 if statecode==06 & ((year==2012 & quarter==1) )
replace SinceBan = -1 if statecode==06 & ((year==2011 & quarter==4) )
replace SinceBan = -2 if statecode==06 & ((year==2011 & quarter==3) )
replace SinceBan = -3 if statecode==06 & ((year==2011 & quarter==2) )
replace SinceBan = -4 if statecode==06 & ((year==2011 & quarter==1))
replace SinceBan = 1 if statecode==06 & ((year==2012 & quarter==2) )
replace SinceBan = 2 if statecode==06 & ((year==2012 & quarter==3) )
replace SinceBan = 3 if statecode==06 & ((year==2012 & quarter==4) )
replace SinceBan = 4 if statecode==06 & ( (year==2013 & quarter==1)) 
replace SinceBan = 5 if statecode==06 & ( (year==2013 & quarter>=2) | year>=2014) 



*Colorado went into effect July 1 2013, so Q3
replace HasBan = 1 if statecode==08 & ((year>=2013 & quarter>=3) | (year>=2014))
replace EverBan = 1 if statecode==08
replace SinceBan = 0 if statecode==08 & ((year==2013 & quarter==3) )
replace SinceBan = -1 if statecode==08 & ((year==2013 & quarter==2) )
replace SinceBan = -2 if statecode==08 & ((year==2013 & quarter==1) )
replace SinceBan = -3 if statecode==08 & ((year==2012 & quarter==4) )
replace SinceBan = -4 if statecode==08 & ((year==2012 & quarter==3))
replace SinceBan = 1 if statecode==08 & ((year==2013 & quarter==4) )
replace SinceBan = 2 if statecode==08 & ((year==2014 & quarter==1) )
replace SinceBan = 3 if statecode==08 & ((year==2014 & quarter==2) )
replace SinceBan = 4 if statecode==08 & ((year==2014 & quarter==3) )
replace SinceBan = 5 if statecode==08 & ((year==2014 & quarter>=4) | year>=2015 )


*Connecticut went into effect Oct 1 2011, so Q4
replace HasBan = 1 if statecode==09 & ((year>=2011 & quarter>=4) | (year>=2012))
replace EverBan = 1 if statecode==09
replace SinceBan = 0 if statecode==09 & ((year==2011 & quarter==4) )
replace SinceBan = -1 if statecode==09 & ((year==2011 & quarter==3) )
replace SinceBan = -2 if statecode==09 & ((year==2011 & quarter==2) )
replace SinceBan = -3 if statecode==09 & ((year==2011 & quarter==1) )
replace SinceBan = -4 if statecode==09 & ((year==2010 & quarter==4))
replace SinceBan = 1 if statecode==09 & ((year==2012 & quarter==1) )
replace SinceBan = 2 if statecode==09 & ((year==2012 & quarter==2) )
replace SinceBan = 3 if statecode==09 & ((year==2012 & quarter==3) )
replace SinceBan = 4 if statecode==09 & ((year==2012 & quarter==4) )
replace SinceBan = 5 if statecode==09 & ((year==2013 & quarter==1) | year>=2013 )


*Delaware's law was signed May 8 2014, but only applies to public employees

replace HasBan = 1 if statecode==10 & ((year>=2014 & quarter>=3) | (year>=2015))
replace EverBan = 1 if statecode==10
replace SinceBan = 0 if statecode==10 & ((year==2014 & quarter==3) )
replace SinceBan = -1 if statecode==15 & ((year==2014 & quarter==2) )
replace SinceBan = -2 if statecode==15 & ((year==2014 & quarter==1) )
replace SinceBan = -3 if statecode==15 & ((year==2013 & quarter==4) )
replace SinceBan = -4 if statecode==15 & ((year==2013 & quarter==3) )
replace SinceBan = 1 if statecode==15 & ((year==2014 & quarter==4) )
replace SinceBan = 2 if statecode==15 & ((year==2015 & quarter==1) )
replace SinceBan = 3 if statecode==15 & ((year==2015 & quarter==2) )
replace SinceBan = 4 if statecode==15 & ((year==2015 & quarter==3) )
replace SinceBan = 5 if statecode==15 & ((year==2015 & quarter>=4) | (year>=2016) )


*Hawaii was effective July 1 2009.
replace HasBan = 1 if statecode==15 & ((year>=2009 & quarter>=3) | (year>=2010))
replace EverBan=1 if statecode==15
replace SinceBan = 0 if statecode==15 & ((year==2009 & quarter==3) )
replace SinceBan = -1 if statecode==15 & ((year==2009 & quarter==2) )
replace SinceBan = -2 if statecode==15 & ((year==2009 & quarter==1) )
replace SinceBan = -3 if statecode==15 & ((year==2008 & quarter==4) )
replace SinceBan = -4 if statecode==15 & ((year==2008 & quarter==3) )
replace SinceBan = 1 if statecode==15 & ((year==2009 & quarter==4) )
replace SinceBan = 2 if statecode==15 & ((year==2010 & quarter==1) )
replace SinceBan = 3 if statecode==15 & ((year==2010 & quarter==2) )
replace SinceBan = 4 if statecode==15 & ((year==2010 & quarter==3) )
replace SinceBan = 5 if statecode==15 & ((year==2010 & quarter>=4) | (year>2010) )


*Illinois was effective Jan 1 2011
replace HasBan = 1 if statecode==17 & ((year>=2011 & quarter>=1) | (year>=2012))
replace EverBan=1 if statecode==17
replace SinceBan = 0 if statecode==17 & ((year==2011 & quarter==1) )
replace SinceBan = -1 if statecode==17 & ((year==2010 & quarter==4) )
replace SinceBan = -2 if statecode==17 & ((year==2010 & quarter==3) )
replace SinceBan = -3 if statecode==17 & ((year==2010 & quarter==2) )
replace SinceBan = -4 if statecode==17 & ((year==2010 & quarter==1) )
replace SinceBan = 1 if statecode==17 & ((year==2011 & quarter==2) )
replace SinceBan = 2 if statecode==17 & ((year==2011 & quarter==3) )
replace SinceBan = 3 if statecode==17 & ((year==2011 & quarter==4) )
replace SinceBan = 4 if statecode==17 & ((year==2012 & quarter==1) )
replace SinceBan = 5 if statecode==17 & ((year==2012 & quarter>=2) | (year>2012))


*Maryland went into effect Oct 1 2011
replace HasBan = 1 if statecode==24 & ((year>=2011 & quarter>=4) | (year>=2012))
replace EverBan=1 if statecode==24
replace SinceBan = 0 if statecode==24 & ((year==2011 & quarter==4) )
replace SinceBan = -1 if statecode==24 & ((year==2011 & quarter==3) )
replace SinceBan = -2 if statecode==24 & ((year==2011 & quarter==2) )
replace SinceBan = -3 if statecode==24 & ((year==2011 & quarter==1) )
replace SinceBan = -4 if statecode==24 & ((year==2010 & quarter==4))
replace SinceBan = 1 if statecode==24 & ((year==2012 & quarter==1) )
replace SinceBan = 2 if statecode==24 & ((year==2012 & quarter==2) )
replace SinceBan = 3 if statecode==24 & ((year==2012 & quarter==3) )
replace SinceBan = 4 if statecode==24 & ((year==2013 & quarter==4))
replace SinceBan = 5 if statecode==24 & ((year==2014 & quarter>=1)| year>2013 )


*Nevada went into effect Oct 1 2013
replace HasBan = 1 if statecode==32 & ((year>=2013 & quarter>=4) | (year>=2014))
replace EverBan = 1 if statecode==32
replace SinceBan = 0 if statecode==32 & ((year==2013 & quarter==4) )
replace SinceBan = -1 if statecode==32 & ((year==2013 & quarter==3) )
replace SinceBan = -2 if statecode==32 & ((year==2013 & quarter==2) )
replace SinceBan = -3 if statecode==32 & ((year==2013 & quarter==1) )
replace SinceBan = -4 if statecode==32 & ((year==2012 & quarter==4))
replace SinceBan = 1 if statecode==32 & ((year==2014 & quarter==1) )
replace SinceBan = 2 if statecode==32 & ((year==2014 & quarter==2) )
replace SinceBan = 3 if statecode==32 & ((year==2014 & quarter==3) )
replace SinceBan = 4 if statecode==32 & ((year==2014 & quarter==4) )
replace SinceBan = 5 if statecode==32 & ((year==2015 & quarter>=1) | (year>2015) )


*Oregon went into effect February 2010
replace HasBan = 1 if statecode==41 & ((year>=2010 & quarter>=2) | (year>=2011))
replace EverBan = 1 if statecode==41
replace SinceBan = 0 if statecode==41 & ((year==2010 & quarter==2) )
replace SinceBan = -1 if statecode==41 & ((year==2010 & quarter==1) )
replace SinceBan = -2 if statecode==41 & ((year==2009 & quarter==4) )
replace SinceBan = -3 if statecode==41 & ((year==2009 & quarter==3) )
replace SinceBan = -4 if statecode==41 & ((year==2009 & quarter==2) )
replace SinceBan = 1 if statecode==41 & ((year==2010 & quarter==3) )
replace SinceBan = 2 if statecode==41 & ((year==2010 & quarter==4) )
replace SinceBan = 3 if statecode==41 & ((year==2011 & quarter==1) )
replace SinceBan = 4 if statecode==41 & ((year==2011 & quarter==2) )
replace SinceBan = 5 if statecode==41 & ((year==2011 & quarter>=3) | (year>2011) )


*Vermont went into effect July 1 2012
replace HasBan = 1 if statecode==50 & ((year>=2012 & quarter>=3) | (year>=2013))
replace EverBan = 1 if statecode==50
replace SinceBan = 0 if statecode==50 & ((year==2012 & quarter==3) )
replace SinceBan = -1 if statecode==50 & ((year==2012 & quarter==2) )
replace SinceBan = -2 if statecode==50 & ((year==2012 & quarter==1) )
replace SinceBan = -3 if statecode==50 & ((year==2011 & quarter==4) )
replace SinceBan = -4 if statecode==50 & ((year==2011 & quarter==3))
replace SinceBan = 1 if statecode==50 & ((year==2012 & quarter==4) )
replace SinceBan = 2 if statecode==50 & ((year==2013 & quarter==1) )
replace SinceBan = 3 if statecode==50 & ((year==2013 & quarter==2) )
replace SinceBan = 4 if statecode==50 & ((year==2013 & quarter==3) )
replace SinceBan = 5 if statecode==50 & ((year==2013 & quarter>=4) | (year>2013))


*Washington went into effect in 2007. Reported on in June, so we will code it as Q3 and on
replace HasBan = 1 if statecode==53 & ((year>=2007 & quarter>=3) | (year>=2008))
replace EverBan=1 if statecode==53
replace SinceBan = 0 if statecode==53 & ((year==2007 & quarter==3) )
replace SinceBan = -1 if statecode==53 & ((year==2007 & quarter==2) )
replace SinceBan = -2 if statecode==53 & ((year==2007 & quarter==1) )
replace SinceBan = -3 if statecode==53 & ((year==2006 & quarter==4) )
replace SinceBan = -4 if statecode==53 & ((year==2006 & quarter==3) )
replace SinceBan = 1 if statecode==53 & ((year==2006 & quarter==4) )
replace SinceBan = 2 if statecode==53 & ((year==2007 & quarter==1) )
replace SinceBan = 3 if statecode==53 & ((year==2007 & quarter==2) )
replace SinceBan = 4 if statecode==53 & ((year==2007 & quarter==3) )
replace SinceBan = 5 if statecode==53 & ((year==2007 & quarter>=4) | (year>2007) )

replace HasBan = (SinceBan>=0 & SinceBan~=.)

*Make indicators
tab SinceBan, gen(IndSinceBan)
forvalues i=1/10{
replace IndSinceBan`i'=0 if IndSinceBan`i'==.
} 

*Create exempted industries
gen exempt1 = 0
replace exempt1 = 1 if industry=="52"
replace exempt1 = 1 if industry=="92"
replace exempt1 = 1 if industry=="55"
replace exempt1 = 1 if industry=="99"


*Drop DE 
drop if statecode==10


*Make interactions
gen BanAffect = HasBan*(1-exempt1)
forvalues i=1(1)10{
gen IndSinceAffect`i' = (1-exempt1)*IndSinceBan`i'
}
gen EverAffect = EverBan*(1-exempt1)

*Make the difference in flow rates
gen jjflow = log(j2jhirer)

*Run the basic regressions using only the states with a ban
* Column 4 in Table 6
reghdfe jjflow BanAffect, absorb(statebytime indbytime id) vce(cluster statecode indgroup)
outreg2 using table_6.xls, dec(3) append
* Column 5 of Table 4 
reghdfe jjflow IndSinceAffect*, absorb(statebytime indbytime id) vce(cluster statecode indgroup)
outreg2 using table_4.xls, dec(3) append




